<div class=" am-padding-xs am-padding-top-0">
    <div class="am-panel am-panel-default">
        <div class="am-panel-bd">
            <div class="am-cf">
                <div class="am-fl am-cf">
                    <?php if (!empty($_GET['back_url'])): ?>
                        <a href="<?= base64_decode($_GET['back_url']) ?>" class="am-margin-right-xs am-text-danger"><i
                                    class="am-icon-reply"></i>返回</a>
                    <?php endif; ?>
                    <strong class="am-text-primary am-text-lg"><a
                                href=""><?= $title; ?></a>
                    </strong> /
                    <small>列表</small>
                </div>
            </div>
            <hr data-am-widget="divider" style="" class="am-divider am-divider-dashed"/>

            <div class="am-scrollable-horizontal">
                <form class="am-form ajax-submit" action="<?= $label->url(GROUP.'-Unit-addFee') ?>" method="POST" data-am-validator>
                    <input type="hidden" name="method" value="GET">
                    <input type="hidden" name="back_url" value="<?= base64_encode($_SERVER['REQUEST_URI']) ?>">
                    <input type="hidden" name="id" value="<?= $label->xss($_GET['id']) ?>">
                    <table class="am-table am-table-bordered  am-table-centered am-text-sm" style="color: #000;min-width: 900px;">
                        <tr class="am-success">
                            <td class="am-text-middle">
                                <input type="text" name="fee_data" data-am-datepicker="{format: 'yyyy-mm', viewMode: 'months', minViewMode: 'months'}" readonly placeholder="抄表月份" required autocomplete="off">
                            </td>
                            <?php for ($i = 1; $i <= $unit_electric_meters; $i++): ?>
                                <td class="am-text-middle" colspan="2">
                                    <input type="text" name="electric_meters_<?=$i?>" placeholder="电表<?=$i?>度数" required autocomplete="off">
                                </td>
                            <?php endfor; ?>
                            <?php for ($i = 1; $i <= $unit_water_meters; $i++): ?>
                                <td class="am-text-middle" colspan="2">
                                    <input type="text" name="water_meters_<?=$i?>" placeholder="水表<?=$i?>度数" required autocomplete="off">
                                </td>
                            <?php endfor; ?>
                            <td class="am-text-middle" style="width: 200px">
                                <button type="submit"><i class="am-icon-plus"></i> 添加水电费</button>
                            </td>
                        </tr>
                        <tr style="font-weight: bold">
                            <td>月份</td>
                            <?php for ($i = 1; $i <= $unit_electric_meters; $i++): ?>
                                <td>电表<?= $i ?></td>
                                <td>电费<?=$i?></td>
                            <?php endfor; ?>
                            <?php for ($i = 1; $i <= $unit_water_meters; $i++): ?>
                                <td>水表<?= $i ?></td>
                                <td>水费<?=$i?></td>
                            <?php endfor; ?>
                            <td>合计</td>
                        </tr>
                        <?php foreach ($list as $key => $value): ?>
                            <tr>
                                <td class="am-text-middle">
                                    <?= $value['date'] ?>
                                    <a href="<?= $label->url(GROUP.'-Unit-delete', ['id' => $label->xss($_GET['id']), 'date' => $value['unit_fee_date'], 'method' =>'GET']) ?>" class="ajax-click ajax-dialog" msg="要删除此记录吗？"><i class="am-icon-remove"></i></a>
                                </td>
                                <?php $total = 0; ?>
                                <?php for ($i = 1; $i <= $unit_electric_meters; $i++): ?>
                                    <?php $electricList[$i] = $value["electric_meters_{$i}"]; ?>
                                    <?php 
									
										$degrees = $value["electric_meters_{$i}"];
									
										if( $value["electric_meters_{$i}"] < $list[$key+1]["electric_meters_{$i}"] ){
											$value["electric_meters_{$i}"] = str_pad(1, strlen($list[$key+1]["electric_meters_{$i}"]) + 1 , '0',STR_PAD_RIGHT) + $value["electric_meters_{$i}"] ;
										}
									
										$electric[$i] = empty($list[$key+1]["electric_meters_{$i}"]) ? '0' : $value["electric_meters_{$i}"] - $list[$key+1]["electric_meters_{$i}"]; 
									
									?>
                                    <td class="am-text-middle"><?= $degrees ?></td>
                                    <td class="am-text-middle am-active">
                                        <?php if(end($list) == $value){continue;} ?>
                                        <?php $total += round($electric[$i] * $unit_electric, 2) ?>
                                        <?= sprintf('%.2f', round($electric[$i] * $unit_electric, 2))  ?> 元
                                        <small class="am-block" style="color: #b5b5b5">(<?= $electric[$i] ?> 度电)</small>
                                    </td>
                                <?php endfor; ?>

                                <?php for ($i = 1; $i <= $unit_water_meters; $i++): ?>
                                    <?php $waterList[$i] = $value["water_meters_{$i}"]; ?>
                                    <?php 
									
										$showWater = $value["water_meters_{$i}"];
									
										if( $value["water_meters_{$i}"] < $list[$key+1]["water_meters_{$i}"] ){
											$value["water_meters_{$i}"] = str_pad(1, strlen($list[$key+1]["water_meters_{$i}"]) + 1 , '0',STR_PAD_RIGHT) + $value["water_meters_{$i}"];
										}
									
										$water[$i] = empty($list[$key+1]["water_meters_{$i}"]) ? '0' : $value["water_meters_{$i}"] - $list[$key+1]["water_meters_{$i}"]; 
									
									?>
                                    <td class="am-text-middle"><?= $showWater ?></td>
                                    <td class="am-text-middle am-active">
                                        <?php if(end($list) == $value){continue;} ?>
                                        <?php $total += round($water[$i] * $unit_water, 2)?>
                                        <?= sprintf('%.2f', round($water[$i] * $unit_water, 2)) ?> 元
                                        <small class="am-block" style="color: #b5b5b5">(<?= $water[$i] ?> 度水)</small>

                                    </td>
                                <?php endfor; ?>
                                <td class="am-text-middle">
                                    <?php if(end($list) == $value){continue;} ?>
                                    <?= sprintf('%.2f', $total) ?> 元
                                    <a href="<?= $label->url(GROUP.'-Unit-invoice', ['name' => $unit_number, 'date' => $value['unit_fee_date'], 'fee' => $unit_fee, 'eFee' => $unit_electric, 'wFee' => $unit_water, 'eList' => implode('-', $electricList), 'wList' => implode('-', $waterList), 'electric' => implode('-', $electric), 'water' => implode('-', $water) ]) ?>" class="am-block show-invoice" data="<?= $value['unit_fee_id'] ?>"><i class="am-icon-print"></i> 收据</a>
                                </td>
                            </tr>

                            <tr class="am-primary">
                                <td colspan="<?= 2 + $unit_electric_meters * 2 + $unit_water_meters * 2 ?>" style="display: none" class="invoice" data="<?= $value['unit_fee_id'] ?>">

                                </td>
                            </tr>

                        <?php endforeach; ?>
                    </table>
                </form>

            </div>


        </div>
    </div>
</div>
<script>
    $(function () {

        $('.show-invoice').on('click', function () {
            var src = $(this).attr('href');
            var id = $(this).attr('data');
            $('.invoice').hide();

            $.get(src, function(data){
                $('.invoice[data="'+id+'"]').html('<img src="data:image/jpg/png/gif;base64,'+data+'" class="am-img-responsive">').show();
            })



            return false;
        })

    })
</script>